package com.ewzj.bigdata.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.di.component.DaggerSexSelectComponent;
import com.ewzj.bigdata.di.module.SexSelectModule;
import com.ewzj.bigdata.mvp.contract.SexSelectContract;
import com.ewzj.bigdata.mvp.presenter.SexSelectPresenter;
import com.ewzj.bigdata.mvp.ui.adapter.SexAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class SexSelectActivity extends BaseActivity<SexSelectPresenter> implements SexSelectContract.View {

    @BindView(R.id.rv_sex)
    RecyclerView rvSex;


    private String sex = "";

    private List<String> sexs;
    private SexAdapter adapter;

    public static void show(Activity activity, String sex) {
        Intent intent = new Intent();
        intent.putExtra("sex", sex);
        intent.setClass(activity, SexSelectActivity.class);
        activity.startActivityForResult(intent, 0x12);
    }


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerSexSelectComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .sexSelectModule(new SexSelectModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_sex_select; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        sex = getIntent().getStringExtra("sex");
        sexs = Arrays.asList(getResources().getStringArray(R.array.sex));
        initialAdapter();
    }

    private void initialAdapter() {
        adapter = new SexAdapter(sexs, sex);
        rvSex.setLayoutManager(new LinearLayoutManager(this));
        rvSex.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent();
            intent.putExtra(AppConstants.EXTRA_DATA, sexs.get(position));
            setResult(RESULT_OK, intent);
            finish();
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}

